
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { getUserByToken } from "../../request/api"


//import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  
  {
    //登录路由没写重定向
    path: '/login',
    name: 'Login',
    component: () => import(/*webpackChunkName:"login" */'../views/Login/login.vue')
  },
 
  // {
  //   path: '/homepage',
  //   name: "HomePage",
  //   component: () => import('../views/HomePage/HomePage.vue'),
  //   children: [
  //     {
  //       path: '/addcategory',
  //       name: 'addcategory',
  //       component: ()=>import('../views/HomePage/children/ContentChild/AddCategory.vue'),
  //     },
  //   ]
  // }
]
  //     {
  //       path: 'category',
  //       component: () => import('../views/HomePage/children/ContentChild/category.vue'),

  //     },
  //     {
  //       path: 'brand',
  //       component: () => import('../views/HomePage/children/ContentChild/brand.vue'),

  //     },
  //     {
  //       path: 'attribute',
  //       component: () => import('../views/HomePage/children/ContentChild/attribute.vue'),

  //     }
  //   ]
  // }
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   // path: '/about',
  //   // name: 'About',
  //   // // route level code-splitting
  //   // // this generates a separate chunk (about.[hash].js) for this route
  //   // // which is lazy-loaded when the route is visited.
  //   // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }


const dynamicRoutes={
  '/good':{
    path: 'good',
    component: () => import('../views/HomePage/children/ContentChild/goods.vue'),

  },
  '/category': {
    path: 'category',
    component: () => import('../views/HomePage/children/ContentChild/category.vue'),
   
  },
  '/brand':{
    path: 'brand',
    component: () => import('../views/HomePage/children/ContentChild/brand.vue'),

  },
  '/attribute':{
    path: 'attribute',
    component: () => import('../views/HomePage/children/ContentChild/attribute.vue'),

  },
  "/addcategory":{
    path:"addcategory",
    component:()=>import('../views/HomePage/children/ContentChild/AddCategory.vue'),
    
  },
  "/editcategory":{
    path:"editcategory/:id",
    component:()=>import ("../views/HomePage/children/ContentChild/Editcategory.vue")
  },
  "/order":{
    path:"order",
    component:()=>import("../views/HomePage/children/ContentChild/Order.vue")
  }
  
 
  
}

const router = new VueRouter({
  routes
})
const addDynameicRoutes=()=>{
  let newRoutes=[]
  for(let i=0;i<store.state.menu.length;i++){
    let path=store.state.menu[i].path;
    if(dynamicRoutes[path]){
      newRoutes.push(dynamicRoutes[path])
    }
  }
  router.addRoutes([
    {
      path:"/homepage",
      component:()=>import ('../views/HomePage/HomePage.vue'),
      children:newRoutes
    },{
      path:"*",
      redirect:"/homepage"
    }
  ])

}
router.beforeEach((to, from, next) => {
  console.log(to,from);
  let token = localStorage.getItem("token")
  if (to.path != '/login' && token && store.state.menu.length == 0) {
    console.log(to,from);
    getUserByToken().then(res => {
      if (res.errno === 0) {
        store.commit("SAVEMENU", res.data.menu)
        addDynameicRoutes()
        next(to)
      } else {
        localStorage.clear()
        next('/login')
      }
    })
  } else if(to.path=='/homepage'&& from.path=='/login'&& store.state.menu.length>0){
    addDynameicRoutes()
    next("/homepage/brand")
  }
  
  else if (!token && to.path != '/login') { 
    next('/login')
  }
  else if(to.path=='/login'&&token){
    next(from)
  }
  else {
    next()
  }

})
export default router
